package com.yzmoney.wechat.action;

import com.yzmoney.common.constant.Constants;
import com.yzmoney.common.constant.PropertyUtils;
import com.yzmoney.common.util.FileUtil;
import com.yzmoney.common.util.HttpClientUtils;
import com.yzmoney.common.util.JDKStackTrace;
import com.yzmoney.common.util.JsonUtils;
import com.yzmoney.common.util.MessageUtil;
import com.yzmoney.wechat.model.general.GeneralReturnCode;
import com.yzmoney.wechat.service.WechatCommonMsgReceiveService;
import com.yzmoney.wechat.service.WechatEventReceiveService;
import com.yzmoney.wechat.service.WechatVerifyService;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller()
@RequestMapping(value = "/test")
public class TestAction {
    private static Logger log = Logger.getLogger(TestAction.class);

    @Autowired
    private WechatVerifyService wechatVerifyService;

    @Autowired
    private WechatEventReceiveService wechatEventReceiveService;

    @Autowired
    private WechatCommonMsgReceiveService wechatCommonMsgReceiveService;
    @Resource
    private com.yzmoney.common.service.StaticService staticService;
    private static String token = PropertyUtils.getValue("TOKEN", "", Constants.GZH_PATH);
    private static String tmpath = PropertyUtils.getValue("", "",
			Constants.QYH_PATH);
    @RequestMapping(value = "/msg")
    public void service(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	String p=FileUtil.getWebInfoPath();
    	String templatePath="/templates/wechat/qyh/qudao_yuyue.ftl";
    	Map model = new HashMap();
    	//String jsonStr = staticService.build(templatePath, model);
    	String jsonStr=FileUtil.readFile(FileUtil.getWebInfoPath()+templatePath);
    	String url="http://m.yzmoney.com/yz_wechat/wechat/sendMsg";
    	
    	httpPost(jsonStr, url);  
    }
    @RequestMapping(value = "/msgRequest")
    public void msgRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	String p=FileUtil.getWebInfoPath();
    	String templatePath="/wechat/qyh/licaishi_yuyue.ftl";
    	Map model = new HashMap();
    	String jsonStr = staticService.build(templatePath, model);
    	//String jsonStr=FileUtil.readFile(FileUtil.getWebInfoPath()+templatePath);
    	String url="http://m.yzmoney.com/yz_wechat/wechat/sendMsg";
    	
    	httpPost(jsonStr, url);  
    }
	private void httpPost(String jsonStr, String url) throws UnsupportedEncodingException,
			IOException, ClientProtocolException {
		HttpPost httpRequst = new HttpPost(url);//创建HttpPost对象
    	List <NameValuePair> params = new ArrayList<NameValuePair>();  
    	params.add(new BasicNameValuePair("tpMsg", jsonStr));
    	//params.add(new BasicNameValuePair("type", "qyh"));
    	httpRequst.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));  
    	HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);  
    	if(httpResponse.getStatusLine().getStatusCode() == 200)  
    	{  
    		HttpEntity httpEntity = httpResponse.getEntity();  
    		String result = EntityUtils.toString(httpEntity);//取出应答字符串  
    	}
	}
    @RequestMapping(value="/dep")
    public void dep(HttpServletRequest request ,HttpServletResponse response) throws Exception{
    	String p=FileUtil.getWebInfoPath();
    	String templatePath="/wechat/qyh/department.ftl";
    	Map model = new HashMap();
    	String jsonStr = staticService.build(templatePath, model);
    	String url="http://m.yzmoney.com/yz_wechat/department/create";
    	httpPost(jsonStr, url); 
    }
}
